package com.example.mapboxdemo;


import static com.mapbox.maps.extension.style.expressions.generated.Expression.heatmapDensity;
import static com.mapbox.maps.extension.style.expressions.generated.Expression.interpolate;
import static com.mapbox.maps.extension.style.expressions.generated.Expression.linear;
import static com.mapbox.maps.extension.style.expressions.generated.Expression.literal;
import static com.mapbox.maps.extension.style.expressions.generated.Expression.rgb;
import static com.mapbox.maps.extension.style.expressions.generated.Expression.rgba;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.maps.LayerPosition;
import com.mapbox.maps.MapView;
import com.mapbox.maps.Style;
import com.mapbox.maps.extension.style.layers.generated.HeatmapLayer;
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource;

public class HeatMapActivity extends AppCompatActivity {

    private static final String EARTHQUAKE_SOURCE_URL = "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson";
    private static final String EARTHQUAKE_SOURCE_ID = "earthquakes";
    private static final String HEATMAP_LAYER_ID = "earthquakes-heat";
    private static final String HEATMAP_LAYER_SOURCE = "earthquakes";
    private static final String CIRCLE_LAYER_ID = "earthquakes-circle";

    private MapView mapView;
    private Style mapboxStyle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heat_map);
        mapView = findViewById(R.id.heatMapView);
        mapView.getMapboxMap().loadStyleUri(Style.TRAFFIC_NIGHT, new Style.OnStyleLoaded() {
            @Override
            public void onStyleLoaded(@NonNull Style style) {

                mapboxStyle = style;
                addEarthquakeSource();
                addHeatmapLayer();
                addCircleLayer();
            }
        });
    }

    private void addEarthquakeSource() {
        GeoJsonSource geoJsonSource = new GeoJsonSource(new GeoJsonSource.Builder(EARTHQUAKE_SOURCE_ID).url(EARTHQUAKE_SOURCE_URL));
        geoJsonSource.bindTo(mapboxStyle);
    }

    private void addHeatmapLayer() {
        HeatmapLayer layer = new HeatmapLayer(HEATMAP_LAYER_ID, EARTHQUAKE_SOURCE_ID);
        layer.maxZoom(9);
        layer.sourceLayer(HEATMAP_LAYER_SOURCE);
        layer.heatmapColor(interpolate(
                linear(), heatmapDensity(),
                literal(0), rgba(33, 102, 172, 0),
                literal(0.2), rgb(103, 169, 207),
                literal(0.4), rgb(209, 229, 240),
                literal(0.6), rgb(253, 219, 199),
                literal(0.8), rgb(239, 138, 98),
                literal(1), rgb(178, 24, 43)
        ));
//        layer.heatmapWeight(interpolate(linear(), get(literal("mag")), stop(0,0), stop(6,1)));
//        layer.heatmapIntensity(interpolate(linear(), zoom(),  stop(0,1), stop(9,3)));
//        layer.heatmapOpacity(interpolate(linear(), zoom(), stop(7,1), stop(9,0)));
//        layer.heatmapRadius(interpolate(linear(), zoom(),stop(0,2), stop(9,20)));
        layer.bindTo(mapboxStyle, new LayerPosition("waterway-label", null, null));
    }

    private void addCircleLayer() {
//        CircleLayer circleLayer = new CircleLayer(CIRCLE_LAYER_ID, EARTHQUAKE_SOURCE_ID);
//        InterpolatorBuilder interpolatorBuilder=new InterpolatorBuilder("");
//        interpolatorBuilder.();
//        new InterpolatorBuilder().
//
//
//        Expression.ExpressionBuilder stop1=new Expression.ExpressionBuilder("stop");
//        stop1.literal(1);
//        stop1.literal(1);
//        circleLayer.circleRadius(interpolate(
//                linear(),
//                zoom(),
//                literal(7), interpolate(
//                        linear(), get("mag"),
//                ),
//                literal(16), interpolate(
//                        linear(), get("mag"), stop(1, 5),
//                        stop(6, 50)
//                )
////        ));
//        circleLayer.circleColor(interpolate(
//                linear(),
//                zoom(),
//                literal(7), interpolate(
//                        linear(), get("mag"), literal(1), rgba(33, 102, 172, 0),
//                        literal(2), rgb(103, 169, 207),
//                        literal(3), rgb(209, 229, 240),
//                        literal(4), rgb(253, 219, 199),
//                        literal(5), rgb(239, 138, 98),
//                        literal(6), rgb(178, 24, 43)
//                )
//        ));
////        circleLayer.circleOpacity(interpolate(
////                linear(), zoom(),
////                stop(7, 0),
////                stop(8, 1)
////        ));
//        circleLayer.circleStrokeColor("white");
//        circleLayer.circleStrokeWidth(1.0f);
//
//        circleLayer.bindTo(mapboxStyle, new LayerPosition(null, HEATMAP_LAYER_ID, null));

    }


    @SuppressLint("Lifecycle")
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null) {
            mapView.onLowMemory();
        }
    }

    @SuppressLint("Lifecycle")
    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null) {
            mapView.onLowMemory();
        }
    }

    @SuppressLint("Lifecycle")
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onLowMemory();
        }
    }

    @SuppressLint("Lifecycle")
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mapView != null) {
            mapView.onLowMemory();
        }
    }
}